package com.dish.action;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.dish.bean.Dish;
import com.dish.bean.MyUsers;
import com.dish.biz.DishService;
import com.dish.biz.UserService;
import com.dish.utils.PageModel;
import com.dish.utils.SafetyControl;
import com.sun.xml.internal.bind.CycleRecoverable.Context;
import com.dish.action.BaseControl;


/**
 * Servlet implementation class LoginControl
 */
@WebServlet("/newLoginControl")
public class NewLoginControl extends BaseControl {
	private int pageSize = 9;// 放到配置文件
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewLoginControl() {
		super();
		// TODO Auto-generated constructor stub
	}

	/***
	 * 登录验证
	 * 
	 * @param request
	 * @param response
	 */
	private void loginCheck(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// step 1 获取用户提交的用户名和口令
		String username = request.getParameter("loginName");
		String password = request.getParameter("loginPass");
		MyUsers loginuser = new MyUsers();
		//SafetyControl sc = new SafetyControl();
		loginuser.setUsername(username);
		loginuser.setPassword(password);
		//loginuser.setPassword(sc.MD5(password));
		// step 2 数据库验证用户
		UserService userserv = new UserService();
		RequestDispatcher rd = null;
		if (userserv.validateUser(loginuser)) {
			//验证通过，记住密码
			if (request.getParameterValues("rem") != null && request.getParameterValues("rem").length == 1){
                Cookie userCookie = new Cookie("myuser", username);  //会覆盖掉原来的
                userCookie.setMaxAge(60);
                Cookie passCookie = new Cookie("mypass", password);
                passCookie.setMaxAge(60);
              //添加cookie
                response.addCookie(userCookie); 
                response.addCookie(passCookie);
            }
			// 验证通过 ，跳转到show.jsp
			HttpSession session = request.getSession(true);
			session.setAttribute("loginuser", loginuser);

			//rd.forward(request, response);
		} else {
			// 否则，重新登录
			response.sendRedirect("login.jsp");
			// request.getRequestDispatcher("login.html").forward(request,
			// response);
		}
	}

	/***
	 * 分页显示
	 * 
	 * @param request
	 * @param response
	 */
	private void pageListView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取当前页号
		int pageNO = Integer.parseInt(request.getParameter("pageNO"));
		// 分页查询, 生成pageModel对象
		DishService dishserv = new DishService();
		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
		// 跳转到show页面
		request.setAttribute("dishlist", pagemodel.getList());
		request.setAttribute("pageModel", pagemodel);
		HttpSession session = request.getSession(true);
		session.setAttribute("dishlist", pagemodel.getList());
		RequestDispatcher rd = request
				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
		rd.forward(request, response);
		
//		// 获取当前页号
//		int pageNO = Integer.parseInt(request.getParameter("pageNO"));
//		// 分页查询,生成pageModel对象
//		DishService dishserv = new DishService();
//		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
//		// 跳转到show页面
//		request.setAttribute("dishlist", pagemodel.getList());
//		request.setAttribute("pageModel", pagemodel);
//		HttpSession session = request.getSession(true);
//		session.setAttribute("dishlist", pagemodel.getList());
//		RequestDispatcher rd = request
//				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
//		rd.forward(request, response);
	}
	
	private void rePageListView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		// 设置当前页号
//		int pageNO = 1;
//		// 分页查询
//		// int pageSize = 6;
//		// 生成pageModel对象
//		DishService dishserv = new DishService();
//		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
//		// 跳转到show页面
//		logger.debug(pagemodel.getList());
//		request.setAttribute("dishlist", pagemodel.getList());
//		request.setAttribute("pageModel", pagemodel);
//		RequestDispatcher rd = request
//				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
//		rd.forward(request, response);
		
		RequestDispatcher rd = null;
		DishService dishserv = new DishService();
		int pageNO = 1;
		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
		request.setAttribute("dishlist", pagemodel.getList());
		request.setAttribute("pageModel", pagemodel);
		rd = request.getRequestDispatcher("show.jsp?pageNO=1&totalpages=" + pagemodel.getTotalPages());
		rd.forward(request, response);

	}


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String actiontype = request.getParameter("actiontype");
		switch (actiontype) {
		case "login":
			// 登录
			loginCheck(request, response);
			break;
		case "pagelist":
			// 分页显示
			pageListView(request, response);
			break;
		case "detail":
			// 显示某一个菜品的详细信息
			break;
		case "cart":
			// 添加到购物车
			break;
		case "reshow":
			rePageListView(request, response);
			break;
		case "addToCart":
			break;
		}
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
